package com.mall.order.biz.handler;

import com.mall.commons.mq.producer.RocketMqProducer;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.mq.DelayCancelOrderProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Description: 利用mq发送延迟取消订单消息
 * @Author： ciggar
 * @Date: 2019-09-17 23:14
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {
    @Autowired
    DelayCancelOrderProducer delayCancelOrderProducer;

    @Autowired
    private RocketMqProducer producer;


    @Override
    public boolean isAsync() {
        return true;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        //todo 1 2
        //tb_stock
        //  1 订单状态设置已取消
        //  2 锁定的库存置零加到库存中
        //tb_order_item
        //  1 status  修改订单状态
        log.info("enter SendMessageHandler start send message...");
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        String orderId = createOrderContext.getOrderId();

        //调用
        delayCancelOrderProducer.sendDelayCannelMessage(orderId);

        try {
            producer.sendOrderMessage(createOrderContext.getOrderId());
        } catch (Exception e) {
            log.error("发送订单id:{}到延迟队列失败", createOrderContext.getOrderId());
            return false;
        }

        return true;
    }
}